package com.situ.stmall.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.situ.stmall.entity.User;


/**
 * 拦截器写法
 * 	创建一个类，实现handlerinterceptor
 * 加上@componet
 * 实现相应方法
 * 创建一个springMvc的配置类，实现webMVCConfig的接口
 * 添加拦截路径和放行路径
 * @author Veigar
 *
 */
@Component
public class UserInterceptor implements HandlerInterceptor{
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// 判断用户登录
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user != null) {
			//用户已登录放行
			return true;
		} else {
			//跳转登录页面
			response.sendRedirect("/user/login");
			return false;
		}
	}
	
}
